package com.example.day05rk.view

import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil.ItemCallback
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.example.lib_base.entity.ViewEntityItem
import com.example.zg3_park.R
import com.example.zg3_park.databinding.ItemViewBinding


class ViewAdapter:PagingDataAdapter<ViewEntityItem, ViewAdapter.ViewViewHolder>(diff) {

    companion object{
        val diff = object :ItemCallback<ViewEntityItem>() {
            override fun areItemsTheSame(
                oldItem: ViewEntityItem,
                newItem: ViewEntityItem
            ): Boolean {
                return oldItem == newItem
            }

            override fun areContentsTheSame(
                oldItem: ViewEntityItem,
                newItem: ViewEntityItem
            ): Boolean {
                return oldItem.id == newItem.id
            }

        }
    }
    class ViewViewHolder(view: View):BaseViewHolder(view) {
        val binding = DataBindingUtil.bind<ItemViewBinding>(view)
    }

    override fun onBindViewHolder(holder: ViewViewHolder, position: Int) {
        val item = getItem(position)
        holder.binding?.view  = item

        holder.binding?.tvStatus?.apply {
            when(item?.visitStatus) {
                "已通过" -> {
                    setBackgroundResource(R.drawable.clolor_pass)
                    setTextColor(holder.itemView.context.resources.getColor(R.color.pass))
                    //setTextColor(R.color.pass)
                }
                "待审核" -> {
                    setBackgroundResource(R.drawable.color_shenhe)
                    setTextColor(holder.itemView.context.resources.getColor(R.color.shenhe))
                }
                "已完成" ->{
                    setBackgroundResource(R.drawable.ccolor_finish)
                    setTextColor(holder.itemView.context.resources.getColor(R.color.finish))
                }
            }
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_view, parent, false)
        return ViewViewHolder(view)
    }
}